import {network} from "../common/network";
import {cmd} from "../common/cmdClient";
import LoadingUI from "../custom/LoadingUI";
import {Config} from "../Config";
import {HINT_TYPE, HintUI} from "../custom/HintUI";
import {GameType} from "../Constants";

export class InfoModule{
    //进入房间
    static enterRoom(data:string){
        LoadingUI.show("进入房间...");
        network.addHandler(cmd.info_main_enter,InfoModule.enterRoomResult,InfoModule);
        network.sendMsg(cmd.info_main_enter,{gameType:data})
    }

    static enterRoomResult(data){
        LoadingUI.exit();
        if(data.code == 0){
            if(data.gameType==GameType.SK){
                Config.NEXT_SCENE = "ShuangKouScene";
                cc.director.loadScene("LoadingScene");
            }else if(data.gameType==GameType.NB_DDZ){
                Config.NEXT_SCENE = "NbDouDiZhuScene";
                cc.director.loadScene("LoadingScene");
            }else{
                HintUI.show("不存在的游戏："+data.gameType);
            }
        }else if(data.code == 1){
            HintUI.show("您还有正在进行的游戏，是否进入？",HINT_TYPE.TWO,function (flag) {
                if(flag){
                    InfoModule.relinkRoom();
                }
            });
        }else{
            HintUI.show(data.errmsg);
        }
    }

    //重连房间
    static relinkRoom(){
        LoadingUI.show("重连中...");
        network.addHandler(cmd.info_main_relink,InfoModule.relinkRoomResult,InfoModule);
        network.sendMsg(cmd.info_main_relink)
    }

    static relinkRoomResult(data){
        LoadingUI.exit();
        if(data.code == 0){
            if(data.gameType==GameType.SK){
                Config.NEXT_SCENE = "ShuangKouScene";
                cc.director.loadScene("LoadingScene");
            }else if(data.gameType==GameType.NB_DDZ){
                Config.NEXT_SCENE = "NbDouDiZhuScene";
                cc.director.loadScene("LoadingScene");
            }else{
                HintUI.show("不存在的游戏："+data.gameType);
            }
        }else{
            HintUI.show(data.errmsg);
        }
    }
}
